package com.building18.service.impl;

import com.building18.handler.MyAssert;
import com.building18.mapper.PermissionMapper;
import com.building18.pojo.Permission;
import com.building18.pojo.User;
import com.building18.pojo.enums.BuildingCode;
import com.building18.pojo.enums.Code;
import com.building18.pojo.vo.PermissionVO;
import com.building18.service.PermissionService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionMapper permissionMapper;
    @Override
    public List<Permission> selectPermission() {
       return permissionMapper.selectPermission();
    }

    @Override
    public HashMap<String, List> getAllPerms(Integer id) {
        //1. 查询所有的一级权限
        List<PermissionVO> firstPerms = permissionMapper.getAllPerms(0);

        //1.2 用现在的一级权限遍历，搜索这些一级权限的二级权限
        for (PermissionVO first : firstPerms) {
            List<PermissionVO> second = permissionMapper.getAllPerms(first.getId());
            //1.3 将当前二级权限集合赋值给一级的children属性
            first.setChildren(second);

            for (PermissionVO s : second) {
                List<PermissionVO> third = permissionMapper.getAllPerms(s.getId());
                s.setChildren(third);
            }
            //递归！！！！！TODO
        }

        List<Integer> allPermsByRid = permissionMapper.getAllPermsByRid(id);  //当前选中角色的所有权限


        List<Integer> pids = new ArrayList<>();
        for (Integer pid : allPermsByRid) {
            Integer parentId = permissionMapper.getParentId(pid);
            pids.add(parentId);                                   //根据所有权限id获取所有父级权限
        }
        allPermsByRid.removeAll(pids);                            //去除父级权限



        HashMap<String, List> map = new HashMap<>();
        map.put("allPermission", firstPerms);                    //所有权限
        map.put("permissionByRid", allPermsByRid);               //当前选中角色的所有子权限
        return map;
    }


    @Override
    @Transactional
    public Integer saveNewPerms(Integer id, List<Integer> ids) {
        // 1. 删除指定角色的权限绑定
        Integer num1 = permissionMapper.deletePermsByRid(id);
        if (!ids.isEmpty()) {
            num1 = permissionMapper.saveNewPerms(id, ids);
        }
        MyAssert.error(num1 <= 0, Code.UPDATE_FAIL.getMessage());
        // 2. 新增指定角色的权限绑定
        return num1;
    }

    @Override
    public Integer updatePermission(Permission permission) {
        return permissionMapper.updatePermission(permission);
    }

    @Override
    @Transactional
    public Integer deletePermission(Integer id) {
        //删关联表的对应权限
        permissionMapper.deleteUidAndPidById(id);
        //删权限
        return permissionMapper.deletePermission(id);
    }

    @Override
    public void addPermsByFile(List<Permission> data) {
        permissionMapper.addPermsByFile(data);
    }

    @Override
    public void beforeDeleteRole(Integer id) {
        List<Integer> permissionList = permissionMapper.beforeDeleteRole(id);
        MyAssert.error(!permissionList.isEmpty(), BuildingCode.ROLE_HAS_PERMISSION_RELATION.getMessage());
    }

    @Override
    public List<Permission> selectAllMenuPerms(Integer id) {
        List<Permission> first = permissionMapper.selectAllMenuPerms(id,0);
        for (Permission permission : first) {
            List<Permission> second = permissionMapper.selectAllMenuPerms(id, permission.getId());
            permission.setChildren(second);
        }
        return first;
    }

    @Override
    public List<String> selectAllButtonPerms() {
        //在非controller层获取session属性
        //1.    (ServletRequestAttributes  Servlet(服务)请求相关的属性)/默认RequestAttributes = 工具类.获取当前属性类的对象
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        //2.   HTTPServlet相关属性 =  HttpServletRequest 和 HttpServletResponse 相关的属性
        HttpServletRequest request = attributes.getRequest();
        //3. 取出Session
        User user = (User) request.getSession().getAttribute("user");
        return permissionMapper.selectAllButtonPerms(user.getId());
    }
}
